﻿@namespace BlazorComponent

@using System.Collections.Generic
@using Microsoft.AspNetCore.Components
@using Radzen

@*
    RadzenImageList: A reusable component to display a list of images with remove functionality.

    Parameters:
    - Images (List<string>): List of image URLs to display.
    - OnRemove (EventCallback<string>): Event callback invoked when an image is removed; passes the URL of the removed image.
*@

<style>
    .rz-image-container {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
    }

    .rz-image-item {
        position: relative;
    }

    .rz-delete-btn {
        position: absolute;
        top: 5px;
        right: 5px;
        width: 24px;
        height: 24px;
        padding: 0;
        border: none;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.5);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 16px;
        cursor: pointer;
    }
</style>

<div class="rz-image-list">
    @if (InternalImages?.Count > 0)
    {
        <div class="rz-image-container">
            @foreach (var item in InternalImages)
            {
                <div class="rz-image-item">
                    <RadzenImage Path="@item.Url"
                                 Style="width:150px; height:150px; object-fit:cover; border-radius:4px;" />
                    <button class="rz-delete-btn" @onclick="() => RemoveImage(item.Id)">
                        &times;
                    </button>
                </div>
            }
        </div>
    }
    else
    {
        <p>No images to display.</p>
    }
</div>

@code {
    public class ImageItem
    {
        public string Id { get; set; }
        public string Url { get; set; }
    }

    [Parameter]
    public List<ImageItem> Images { get; set; } = new List<ImageItem>();

    [Parameter]
    public EventCallback<string> OnRemove { get; set; }

    private List<ImageItem> InternalImages { get; set; } = new List<ImageItem>();

    protected override void OnParametersSet()
    {
        InternalImages = Images != null
            ? new List<ImageItem>(Images)
            : new List<ImageItem>();
    }

    private async Task RemoveImage(string id)
    {
        var item = InternalImages.Find(x => x.Id == id);
        if (item != null)
        {
            InternalImages.Remove(item);
            await OnRemove.InvokeAsync(id);
        }
    }
}